<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>选择收货地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/common_jmc.css"/>
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/jmc_icon.css"/>
    <link rel="stylesheet" type="text/css" href="$!webPath/resources/app/mcjd/css/jmc_ufm.css"/>

    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/template.js"></script>
    <script type="text/javascript">
        window._URI_ = {
            IP0: '$!mallBaseurl', // 本地
            IP1: 'http://testmall.yijiahn.com/mall' // 开发环境
        }
        window._URL_ = {
            addAddress: _URI_.IP0 + '/app/mcjd/addAddress.htm', // 新增地址
            editAddress: _URI_.IP0 + '/app/mcjd/editAddress.htm', // 编辑地址
            addrList: _URI_.IP0 + '/app/user/address/list.json', // 地址列表
            delAddr: _URI_.IP0 + '/app/user/address/del.json', // 删除地址
            settlement: _URI_.IP0 + '/app/mcjd/settlement.htm', // 结算
            buynow: _URI_.IP0 + '/app/mcjd/buynow.htm' //立即购买
        };
    </script>
</head>
<body style="background:#fafafa;">

    <div id="bxTopbar">
        <div class="bx_topbar">
            <div class="h_44"></div>
            <div class="wp_topbar">
                <div class="it_btn it_back_btn"><i class="icon ico_1"></i></div>
                <a class="it_btn it_sure_btn" href="javascript:;" style="visibility:hidden;">确定</a>
                <div class="it_toptt">选择收货地址</div>
            </div>
        </div>
    </div>

    <div class="bx-addrlist">
        <div class="wp-addrlist">
            <ul class="list-addr" id="resAddrList"></ul>

            <div id="resNoData"><div class="no_loaddata"></div></div>
        </div>
    </div>

    <div class="addAddress">
        <div class="h_48"></div>
        <div class="lk_addaddr">添加新地址</div>
    </div>


    <div class="overlay__bg" style="display:none;position:fixed;top:0;bottom:0;right:0;left:0;z-index:999999;background:rgba(255,255,255,0.15);"></div>

    <!-- 模板 -->
    <script type="text/html" id="tmplAddrList">
        {{each list as item ind}}
        <li class="item">
            <div class="it-info">
                <div class="it-sel" id="addr_{{item.addressId}}" it_addressId="{{item.addressId}}"></div>
                <div class="it-user">
                    <div class="it-tt clearfix"><span class="fl">{{item.receiverName}}</span><span class="fr">{{item.mobile}}</span></div>
                    <div class="it-addr">
						{{if item.isdefault == 1}}
						<b>[默认]</b>
						{{/if}}
						{{item.provinceName +" "+item.cityName +" "+item.countyName +" "+item.streetName +" "+item.detailAddress}}
					</div>
                </div>
            </div>
            <div class="it-btns">
                <span class="ibtn ibtn_ico_22 it_edit" it_addressInfo="{{pareAddress(item)}}">编辑</span>
                <span class="ibtn ibtn_ico_23 it_del" it_addressId="{{item.addressId}}">删除</span>
            </div>
        </li>
        {{/each}}
    </script>
    <script type="text/html" id="tmplNoData">
        <div class="no_loaddata"></div>
    </script>
    <script type="text/html" id="tmplErrorData">
        <div class="no_errordata">
            <div class="error_refresh_btn"><img src="images/ico_7.png" width="26" /><p>出错了，点击刷新</p></div>
        </div>
    </script>
    <script type="text/javascript">
        template.helper("getArrNum", function(val){
            var n = val.length || 0;
            return n;
        });
        template.helper("pareAddress", function(val){
            var addressInfo = "&a_receiverName="+val.receiverName
            	+"&a_mobile="+val.mobile
            	+"&a_cityId="+val.cityId
            	+"&a_cityName="+val.cityName
            	+"&a_provinceId="+val.provinceId
            	+"&a_provinceName="+val.provinceName
            	+"&a_countyId="+val.countyId
            	+"&a_countyName="+val.countyName
            	+"&a_streetId="+val.streetId
            	+"&a_streetName="+val.streetName
            	+"&a_detailAddress="+val.detailAddress
            	+"&a_addressId="+val.addressId
            	+"&a_isdefault="+val.isdefault
            	+"&a_detail="+val.detail;
            return addressInfo;
        });
    </script>

    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/touch.min.js"></script>
    <script type="text/javascript" src="$!webPath/resources/app/mcjd/js/utils.js"></script>
    <script>
        $(function() {
        	var _accessToken = "$!accessToken",_orgId="$!orgId",_goodsCartIds="$!goodsCartIds",_addressId="$!addressId";
        	var _isBuynow = "$!isBuynow",_buyParams="$!buyParams",_goodsId="$!goodsId";
        	
            // 获取地址列表
            function getAddrList() {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    timeout: 5000,
                    url: _URL_.addrList,
                    headers: {},
                    data: {
                    	accessToken: _accessToken,
                    	orgId: _orgId
                    },
                    success: function(res) {
                        $('.overlay__bg').hide(0);
                        if ("100" === res.code) { //请求成功
                            var html = template('tmplAddrList', res.data);
                            $('#resAddrList').html(html);
                            $("#addr_"+_addressId).addClass("s__checked");
                        } else if ("401" === res.code) { //App端请求，需要登录
                            Utils.showToast("请登录后再操作！");
                        } else if ("405" === res.code) { //微信端请求，需要登录
                            Utils.showToast("请先微信授权后再操作！");
                            WxUtils.goWxPage(window.location.href, 1000);
                        } else {
                            console.log('请求出错: ' + _URL_.goodsDetailById);
                            Utils.showToast(res.code + ":" + res.msg || '请求出错');
                        }
                    },
                    error: function(xhr) {
                        $('.overlay__bg').hide(0);
                        console.log('请求出错: ' + _URL_.goodsDetailById);
                        var msg = "请求出错(请检查相关网络状况)";
                        Utils.showToast(msg);
                    }
                });
            };
            getAddrList();

            // 删除地址
            function delAddr(_addressId) {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    timeout: 5000,
                    url: _URL_.delAddr,
                    headers: {},
                    data: {
                    	accessToken: _accessToken,
                    	orgId: _orgId,
                    	addressId: _addressId
                    },
                    success: function(res) {
                        $('.overlay__bg').hide(0);
                        if ("100" === res.code) { //请求成功
                            location.reload();
                        } else if ("401" === res.code) { //App端请求，需要登录
                            Utils.showToast("请登录后再操作！");
                        } else if ("405" === res.code) { //微信端请求，需要登录
                            Utils.showToast("请先微信授权后再操作！");
                            WxUtils.goWxPage(window.location.href, 1000);
                        } else {
                            console.log('请求出错: ' + _URL_.goodsDetailById);
                            Utils.showToast(res.code + ":" + res.msg || '请求出错');
                        }
                    },
                    error: function(xhr) {
                        $('.overlay__bg').hide(0);
                        console.log('请求出错: ' + _URL_.goodsDetailById);
                        var msg = "请求出错(请检查相关网络状况)";
                        Utils.showToast(msg);
                    }
                });
            };

            // 地址列表--选中、编辑、删除
            $('#resAddrList').on('tap', '.it-sel', function(e) {
            	$(".s__checked").removeClass("s__checked");
                $(this).addClass('s__checked');
                _addressId = $(this).attr("it_addressId");
            }).on('tap', '.it_edit', function(e) {
            	var addressinfo = $(this).attr("it_addressinfo");
                location.href = _URL_.editAddress +"?accessToken="+_accessToken +"&orgId="+_orgId +"&goodsCartIds="+_goodsCartIds +"&addressId="+_addressId +"&addressInfo="+encodeURI(addressinfo);
            }).on('tap', '.it_del', function(e) {
                var addressId = $(this).attr("it_addressId");
                delAddr(addressId);
            });
            // 新增地址
            $('.addAddress').click(function(){
            	location.href = _URL_.addAddress +"?accessToken="+_accessToken +"&orgId="+_orgId +"&goodsCartIds="+_goodsCartIds +"&addressId="+_addressId;
            });
            
            //返回上一页
            $('.it_back_btn').click(function(){
            	if(_isBuynow == "1"){
		        	location.href = _URL_.buynow + "?accessToken="+_accessToken +"&orgId="+_orgId +"&params="+_buyParams +"&addressId="+_addressId +"&goodsId="+_goodsId;
            	}else{
		        	location.href = _URL_.settlement + "?accessToken="+_accessToken +"&orgId="+_orgId +"&goodsCartIds="+_goodsCartIds +"&addressId="+_addressId;
            	}
            });
        });
    </script>

</body>
</html>